package attendance

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/attendance"
	attendanceReq "github.com/flipped-aurora/gin-vue-admin/server/model/attendance/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SignRecordApi struct {
}

var signreService = service.ServiceGroupApp.AttendanceServiceGroup.SignRecordService

// CreateSignRecord 创建考勤记录
// @Tags SignRecord
// @Summary 创建考勤记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.SignRecord true "创建考勤记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /signre/createSignRecord [post]
func (signreApi *SignRecordApi) CreateSignRecord(c *gin.Context) {
	var signre attendance.SignRecord
	err := c.ShouldBindJSON(&signre)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	signre.BelongTo = utils.GetBelongTo(c)
	if err := signreService.CreateSignRecord(&signre); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteSignRecord 删除考勤记录
// @Tags SignRecord
// @Summary 删除考勤记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.SignRecord true "删除考勤记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /signre/deleteSignRecord [delete]
func (signreApi *SignRecordApi) DeleteSignRecord(c *gin.Context) {
	ID := c.Query("ID")
	if err := signreService.DeleteSignRecord(ID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteSignRecordByIds 批量删除考勤记录
// @Tags SignRecord
// @Summary 批量删除考勤记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /signre/deleteSignRecordByIds [delete]
func (signreApi *SignRecordApi) DeleteSignRecordByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := signreService.DeleteSignRecordByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateSignRecord 更新考勤记录
// @Tags SignRecord
// @Summary 更新考勤记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.SignRecord true "更新考勤记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /signre/updateSignRecord [put]
func (signreApi *SignRecordApi) UpdateSignRecord(c *gin.Context) {
	var signre attendance.SignRecord
	err := c.ShouldBindJSON(&signre)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := signreService.UpdateSignRecord(signre); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindSignRecord 用id查询考勤记录
// @Tags SignRecord
// @Summary 用id查询考勤记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query attendance.SignRecord true "用id查询考勤记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /signre/findSignRecord [get]
func (signreApi *SignRecordApi) FindSignRecord(c *gin.Context) {
	ID := c.Query("ID")
	if resignre, err := signreService.GetSignRecord(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"resignre": resignre}, c)
	}
}

// GetSignRecordList 分页获取考勤记录列表
// @Tags SignRecord
// @Summary 分页获取考勤记录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query attendanceReq.SignRecordSearch true "分页获取考勤记录列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /signre/getSignRecordList [get]
func (signreApi *SignRecordApi) GetSignRecordList(c *gin.Context) {
	var pageInfo attendanceReq.SignRecordSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if list, total, err := signreService.GetSignRecordInfoList(pageInfo, utils.GetBelongTo(c)); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetSignRecordPublic 不需要鉴权的考勤记录接口
// @Tags SignRecord
// @Summary 不需要鉴权的考勤记录接口
// @accept application/json
// @Produce application/json
// @Param data query attendanceReq.SignRecordSearch true "分页获取考勤记录列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /signre/getSignRecordPublic [get]
func (signreApi *SignRecordApi) GetSignRecordPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的考勤记录接口信息",
	}, "获取成功", c)
}
